package com.example.petadopt.controller;

import com.example.petadopt.entity.AdoptApply;
import com.example.petadopt.entity.AdoptionApplication;
import com.example.petadopt.entity.Dictionary;
import com.example.petadopt.entity.Pet;
import com.example.petadopt.service.AdoptApplyService;
import com.example.petadopt.service.DictionaryService;
import com.example.petadopt.controller.vo.Result;
import com.example.petadopt.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/v1/applications")
public class AdoptApplyController {
    @Autowired
    private AdoptApplyService adoptApplyService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private PetService petService;

    // 获取所有领养申请
    @RequestMapping("/findAll")
    public ResponseEntity<Result> getApplications(@RequestBody Map<String, String> userInfo) {
        try {
            String userId = userInfo.get("id");
            int id= Integer.parseInt(userId);
//            System.out.println("yonghu: " + userId); // 打印返回的数据
            List<Pet> applications = petService.getPetWithAdoptApplyByUserId(id);
            return new ResponseEntity<>(new Result(2000, "查询成功", applications), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new Result(5000, "服务器内部错误", null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 审核领养申请
    @PostMapping("/{id}/review")
    public ResponseEntity<Result> reviewApplication(@PathVariable int id, @RequestParam int statusId) {
        try {
            System.out.println("审核请求参数: id=" + id + ", statusId=" + statusId); // 打印请求参数
            // 获取申请
            Optional<AdoptionApplication> applicationOptional = adoptApplyService.findById(id);
            if (!applicationOptional.isPresent()) {
                return new ResponseEntity<>(new Result(4003, "申请不存在", null), HttpStatus.NOT_FOUND);
            }

            // 获取状态
            Optional<Dictionary> statusOptional = dictionaryService.findById(statusId);
            if (!statusOptional.isPresent()) {
                return new ResponseEntity<>(new Result(4004, "无效的状态ID", null), HttpStatus.BAD_REQUEST);
            }
            // 更新申请状态
            AdoptionApplication application = applicationOptional.get();
            Dictionary status = statusOptional.get();
            application.setStatusId(statusId);
            adoptApplyService.save(application);

            // 返回成功结果
            return new ResponseEntity<>(new Result(2000, "审核成功", application), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new Result(5000, "服务器内部错误", null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}